package cn.lin.task.action;

import java.lang.reflect.InvocationTargetException;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sun.org.apache.commons.beanutils.BeanUtils;

import cn.lin.task.model.Task;
import cn.lin.task.model.User;
import cn.lin.task.service.TaskService;
import cn.lin.task.service.UserService;
@SuppressWarnings("serial")
@Controller("userAction")
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven<User>{
	private UserService userService;
	private TaskService taskService;
	private User user;
	
	public UserService getUserService() {
		return userService;
	}
	@Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public TaskService getTaskService() {
		return taskService;
	}
	@Resource
	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	
	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		if(user==null) user=new User();
		return user;
	}
	
	public String loginInput() {
		return SUCCESS;
	}
	
	
	public void validateLogin() {
		if (user.getStuID() == null || user.getStuID().equals("")) {
			this.addFieldError("stuID", "学号不能为空");
		}
		if (user.getPassword() == null || user.getPassword().equals("")) {
			this.addFieldError("password", "密码不能为空");
		}
		if (this.hasFieldErrors()) {
			loginInput();
			return;
		}
		User dataUser = userService.load(user.getStuID());
		this.clearFieldErrors();
		if (dataUser == null) {
			this.addFieldError("stuID", "用户不存在");
		} else if (!dataUser.getPassword().equals(user.getPassword())) {
			this.addFieldError("password", "密码错误");
		}
		if (this.hasFieldErrors()) {
			loginInput();
		}
	}
	
	public String login() {
		User dataUser = userService.load(user.getStuID());
		ActionContext.getContext().put("url", "/user_userCMD.action?uid="+dataUser.getUid()+"&pass="+dataUser.getPassword());
		return "redirect";
	}
	
//	
	public boolean passUserCMD() {
		if(ActionContext.getContext().getParameters().get("uid")==null){
			return false;
		}
		int uid=Integer.parseInt((((String[])ActionContext.getContext().getParameters().get("uid"))[0]));
		User usere=userService.load(uid);
		//System.out.println(uid);
		if(ActionContext.getContext().getParameters().get("pass")==null) {
			return false;
		}
		String pass=(((String[])ActionContext.getContext().getParameters().get("pass"))[0]);
		//System.out.println(pass);
		if(pass==null) {
			return false;
		}else if(usere==null) {
			return false;
		}else if(!pass.equals(usere.getPassword())) {
			return false;
		}else {
			return true;
		}
	}
//	
	public String userCMD() {
		if(passUserCMD()) {
		int uid=Integer.parseInt((((String[])ActionContext.getContext().getParameters().get("uid"))[0]));
		User usere=userService.load(uid);
		ActionContext.getContext().put("us",usere);
		return SUCCESS;
		}else {
			ActionContext.getContext().put("url", "/user_loginInput.action");
			return "redirect";
		}
	}
	
	public String addInput() {
		return SUCCESS;
	}
	
	public void validateAdd() {
		User tem = userService.load(user.getStuID());
		System.out.println(tem);
		if (tem != null) {
			this.addFieldError("stuID", "学号已被注册");
			
		}
		if(this.hasFieldErrors()) {
			addInput();
		}
	}

	public String add() {
		//System.out.println(user);
		userService.add(user);
		ActionContext.getContext().put("url", "/user_loginInput.action");
		return "redirect";
	}

	public boolean pass() {
		int uid=user.getUid();
		String password=user.getPassword();
		System.out.println(uid+" "+password);
		if(password==null) {
			return false;
		}else if(!password.equals(userService.load(uid).getPassword())) {
			return false;
		}else {
			return true;
		}
		
		
	}
	public String updateInput() {
		if (pass()) {
			User tar = userService.load(user.getUid());
			//System.out.println(tar);
			try {
				BeanUtils.copyProperties(user, tar);
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return SUCCESS;
		} else {
			ActionContext.getContext().put("url", "/user_loginInput.action");
			return "redirect";
		}
	}
	
	public String update() {
		try {
			userService.update(user);
			ActionContext.getContext().put("url", "/user_userCMD.action?uid="+user.getUid()+"&pass="+user.getPassword());
			return "redirect";
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return "error";
		}
		
	}
	
	public String tasklist() {
		if (passUserCMD()) {
			int uid = Integer.parseInt((((String[]) ActionContext.getContext().getParameters().get("uid"))[0]));
			ActionContext.getContext().put("user", userService.load(uid));
			ActionContext.getContext().put("ut", taskService.find(userService.load(uid)));
			return SUCCESS;
		} else {
			ActionContext.getContext().put("url", "/user_loginInput.action");
			return "redirect";
		}
	}
	
	public String taskwait() {
		int tid=Integer.parseInt((((String[])ActionContext.getContext().getParameters().get("tid"))[0]));
		//System.out.println("tid"+tid);
		Task task=taskService.load(tid);
		task.setIsFinish(false);
		taskService.update(task);
		int uid=Integer.parseInt((((String[])ActionContext.getContext().getParameters().get("uid"))[0]));
		User user=userService.load(uid);
		ActionContext.getContext().put("url", "/user_tasklist.action?uid="+user.getUid()+"&pass="+user.getPassword());
		return "redirect";
	}
	
	public String taskfinish() {
		int tid=Integer.parseInt((((String[])ActionContext.getContext().getParameters().get("tid"))[0]));
		//System.out.println("tid"+tid);
		Task task=taskService.load(tid);
		task.setIsFinish(true);
		taskService.update(task);
		int uid=Integer.parseInt((((String[])ActionContext.getContext().getParameters().get("uid"))[0]));
		User user=userService.load(uid);
		ActionContext.getContext().put("url", "/user_tasklist.action?uid="+user.getUid()+"&pass="+user.getPassword());
		return "redirect";
	}
}
